package com.tebiecloud.mpweixin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.common.utils.UUIDUtil;
import com.tebiecloud.mpweixin.client.constants.MpweixinConstants;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserFlow;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserWallet;
import com.tebiecloud.mpweixin.client.model.vo.FundsFlowListVO;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserFlowMapper;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserWalletMapper;
import com.tebiecloud.mpweixin.server.service.MpweixinUserFlowService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户资金记录表 服务实现类
 * @author tebie
 * @date 2019-09-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MpweixinUserFlowServiceImpl extends BaseServiceImpl<MpweixinUserFlowMapper, MpweixinUserFlow> implements MpweixinUserFlowService {

    @Autowired
    MpweixinUserFlowMapper mpweixinUserFlowMapper;

    @Autowired
    MpweixinUserWalletMapper mpweixinUserWalletMapper;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public IPage<FundsFlowListVO> selectFundsFlowList(Integer page, Integer limit) {
        IPage<FundsFlowListVO> fundsFlowListVOIPage = new Page<>();
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        if(securityUserDetails!=null && securityUserDetails.getUserId()!=null){
            QueryWrapper<MpweixinUserFlow> mpweixinUserFlowQueryWrapper = new QueryWrapper<MpweixinUserFlow>();
            mpweixinUserFlowQueryWrapper.eq("user_id",securityUserDetails.getUserId());
            mpweixinUserFlowQueryWrapper.orderByDesc("ct");
            IPage<MpweixinUserFlow> mpweixinUserFlowIPage = mpweixinUserFlowMapper.selectPage(new Page<MpweixinUserFlow>(page,limit),mpweixinUserFlowQueryWrapper);
            fundsFlowListVOIPage.setCurrent(mpweixinUserFlowIPage.getCurrent());
            fundsFlowListVOIPage.setPages(mpweixinUserFlowIPage.getPages());
            fundsFlowListVOIPage.setSize(mpweixinUserFlowIPage.getSize());
            fundsFlowListVOIPage.setTotal(mpweixinUserFlowIPage.getTotal());
            if(mpweixinUserFlowIPage.getPages()>0){
                List<FundsFlowListVO> fundsFlowListVOS = new ArrayList<>();
                for(MpweixinUserFlow mpweixinUserFlow:mpweixinUserFlowIPage.getRecords()){
                    FundsFlowListVO fundsFlowListVO = new FundsFlowListVO();
                    fundsFlowListVO.setCt(DateUtils.formatDate(mpweixinUserFlow.getCt(),"yyyy-MM-dd HH:mm:ss"));
                    fundsFlowListVO.setNumber(mpweixinUserFlow.getNumber());
                    fundsFlowListVO.setTransactionFunds(mpweixinUserFlow.getTransactionFunds());
                    fundsFlowListVO.setTransactionType(mpweixinUserFlow.getTransactionType());
                    fundsFlowListVOS.add(fundsFlowListVO);
                }
                fundsFlowListVOIPage.setRecords(fundsFlowListVOS);
            }
        }
        return fundsFlowListVOIPage;
    }

    @Override
    public Map<String, String> saveFlow(Long userId, Long number, int amount, int type) {
        Map<String, String> resultMap = new HashMap<String, String>();
        int time = 10;
        String key = MpweixinConstants.REDIS_USERWALLET_+userId;
        boolean isLock = redisUtils.lock(key, time);
        if(!isLock){
            throw new RuntimeException("资金池被占用，请稍等");
        }
        //查询钱包
        QueryWrapper<MpweixinUserWallet> mpweixinUserWalletQueryWrapper = new QueryWrapper<>();
        mpweixinUserWalletQueryWrapper.eq("user_id",userId);
        MpweixinUserWallet mpweixinUserWallet =mpweixinUserWalletMapper.selectOne(mpweixinUserWalletQueryWrapper);
        //计算交易的资金
        int futureFunds = 0;
        if(type==1||type==2||type==5||type==6){
            futureFunds = mpweixinUserWallet.getBalance() + amount;
        }
        if(type==3||type==4){
            futureFunds = mpweixinUserWallet.getBalance() - amount;
        }
        MpweixinUserFlow mpweixinUserFlow = new MpweixinUserFlow();
        mpweixinUserFlow.setUserId(userId);
        mpweixinUserFlow.setNumber(number);
        mpweixinUserFlow.setTransactionType(type);
        mpweixinUserFlow.setFutureFunds(futureFunds);
        mpweixinUserFlow.setResidueFunds(mpweixinUserWallet.getBalance());
        mpweixinUserFlow.setTransactionFunds(amount);
        mpweixinUserFlowMapper.insert(mpweixinUserFlow);
        //解锁
        redisUtils.unLock(key);
        return resultMap;
    }


}
